package com.WISSC.util.file;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * <p>
 *
 * </p>
 *
 * @author 张凯
 * @since 2019/6/3
 */
public class Picture {

    /**
     * @lqc
     * @根据指定的str去生成图片
     */
    public static byte[] createImage(String str, Font 黑体, File outFile) throws Exception {
        Font font = new Font("黑体", Font.BOLD, 18);
        //获取font的样式应用在str上的整个矩形
        Rectangle2D r = font.getStringBounds(str, new FontRenderContext(AffineTransform.getScaleInstance(2, 2), false, false));
        //获取单个字符的高度
        int unitHeight = (int) Math.floor(r.getHeight());
        //获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
        int width = (int) Math.round(r.getWidth()) + 1;
        //把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
        int height = 1000;
        //创建图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics g = image.getGraphics();
        g.setColor(Color.WHITE);
        //先用白色填充整张图片,也就是背景
        g.fillRect(0, 0, width, height);
        //在换成黑色
        g.setColor(Color.black);
        //设置画笔字体
        g.setFont(font);
        //画出字符串
        String[] split = str.split("=");
        int y = 50;
        for (int i = 0; i < split.length; i++) {
            if (i == 0){
                g.drawString(split[i], 0, y);
            }else{
                g.drawString(split[i], 0, y);
            }
            y += 50;
        }
        g.dispose();

        if (outFile != null) {
            //输出png图片到指定目录
            ImageIO.write(image, "jpg", outFile);
        }
        //返回数据流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        boolean flag = ImageIO.write(image, "jpg", out);
        byte[] b = out.toByteArray();
        return b;

    }

}
